package org.darcy.youchat.activity.message;

import android.text.TextUtils;
import android.view.View;

import com.netease.nimlib.sdk.team.constant.TeamFieldEnum;
import com.netease.nimlib.sdk.team.model.Team;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.databinding.ActivityTeamAnnouncementEditBinding;
import org.darcy.youchat.nimsdk.NimTeamSDK;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 编辑群公告界面
 */
public class TeamAnnouncementEditActivity extends BaseActivity {

    private ActivityTeamAnnouncementEditBinding binding;

    public static final String TEAM = "team";
    private Team mTeam;

    @Override
    public void init() {
        mTeam = (Team) getIntent().getSerializableExtra(TEAM);
        if (mTeam == null) {
            interrupt();
        }
    }

    @Override
    public void initView() {
        binding = ActivityTeamAnnouncementEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setTitle("编辑群公告");
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);

        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);
        binding.mToolbar.btnOk.setText("完成");

        binding.mToolbar.btnOk.setOnClickListener(v -> {
            final String content = binding.etContent.getText().toString().trim();
            if (!TextUtils.isEmpty(content)) {
                showMaterialDialog("", "该公告会通知全部群成员，是否发布?", "发布", "取消", v1 -> {
                    hideMaterialDialog();
                    Map<TeamFieldEnum, Serializable> fields = new HashMap<>(1);
                    fields.put(TeamFieldEnum.Announcement, content);
                    NimTeamSDK.updateTeamFields(mTeam.getId(), fields);
                    showWaitingDialog("正在保存");
                    //TODO:@所有人
                    finish();
                }, v12 -> hideMaterialDialog());

            }
        });
    }




}
